<%@ page import="org.example.demo1.Entity.User" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>书羊网 - 作者工作台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        /* 工作台样式 */
        .workspace-card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            border: none;
            border-radius: 12px;
        }
        
        .workspace-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0,0,0,0.15);
        }
        
        .stats-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 15px;
        }
        
        .stats-number {
            font-size: 2.5rem;
            font-weight: bold;
        }
        
        .novel-item {
            border-left: 4px solid #007bff;
            transition: all 0.3s ease;
        }
        
        .novel-item:hover {
            border-left-color: #28a745;
            background-color: #f8f9fa;
        }
        
        .status-badge {
            font-size: 0.75rem;
        }
        
        .action-btn {
            border-radius: 20px;
            padding: 0.375rem 1rem;
        }
        
        .section-title {
            position: relative;
            margin-bottom: 25px;
            padding-left: 15px;
            font-weight: bold;
            border-left: 4px solid #e74c3c;
        }
        
        .quick-action {
            background: linear-gradient(45deg, #28a745, #20c997);
            border: none;
            color: white;
            border-radius: 25px;
            padding: 12px 24px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .quick-action:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(40, 167, 69, 0.4);
            color: white;
        }
        
        /* 新增：分卷和章节样式 */
        .volume-header {
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 12px;
        }
        
        .chapter-item {
            transition: all 0.3s ease;
            border-left: 3px solid #dee2e6;
        }
        
        .chapter-item:hover {
            border-left-color: #007bff;
            background-color: #f8f9fa;
            transform: translateX(5px);
        }
        
        .cursor-pointer {
            cursor: pointer;
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
    <div class="container">
        <a class="navbar-brand py-0" href="#">
            <img src="https://mysteriouslamb.oss-cn-hangzhou.aliyuncs.com/icon.jpg" alt="" width="30" height="30">
            书羊网
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav me-auto">
                <li class="nav-item px-1">
                    <a class="nav-link" href="${pageContext.request.contextPath}/MainPage/functionPage/index.jsp">首页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="${pageContext.request.contextPath}/MainPage/functionPage/library.jsp">书库</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="${pageContext.request.contextPath}/MainPage/functionPage/bookshelf.jsp">书架</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">角色扮演</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" aria-current="page" href="#">作者工作台</a>
                </li>
            </ul>
            <form class="d-flex mx-auto" style="width: 40%; max-width: 400px;">
                <input class="form-control me-2 rounded-pill" type="search" placeholder="请输入书名或作者名" aria-label="Search">
                <button class="btn btn-outline-success rounded-pill" type="submit" style="min-width: 60px;">搜索</button>
            </form>
            <div class="ms-auto d-flex align-items-center gap-3 me-3">
                <a class="d-flex align-items-center text-decoration-none" href="#" style="gap:12px">
                    <img src="${user.avatar_url}" alt="用户头像"
                         class="rounded-circle shadow-sm transition-all"
                         width="45" height="45"
                         style="transition:0.3s">
                    <div class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle fw-bold text-dark p-2 rounded-3"
                           href="#"
                           id="navbarDropdown"
                           role="button"
                           data-bs-toggle="dropdown"
                           aria-expanded="false"
                           style="background-color: rgba(0,0,0,0.05); transition: 0.3s;">
                            ${user.username}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end shadow border-0 rounded-3" aria-labelledby="navbarDropdown">
                            <li><a class="dropdown-item text-danger" href="${pageContext.request.contextPath}/API/user/logout">
                                <i class="bi bi-door-open me-2"></i>退出登录
                            </a></li>
                        </ul>
                    </div>
                </a>
            </div>
        </div>
    </div>
</nav>

<!-- 主要内容区域 -->
<div class="container" style="margin-top: 80px; padding-top: 20px;">
    <!-- 欢迎区域 -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card workspace-card shadow-sm">
                <div class="card-body p-4">
                    <div class="row align-items-center">
                        <div class="col-md-8">
                            <h2 class="mb-2">欢迎回来，${user.username}！</h2>
                            <p class="text-muted mb-3">在这里管理您的作品，与读者互动，追踪创作数据</p>
                            <button class="btn quick-action me-3" onclick="showCreateNovelModal()">
                                <i class="bi bi-plus-circle me-2"></i>创建新作品
                            </button>
                            <button class="btn btn-outline-primary action-btn" onclick="loadDrafts()">
                                <i class="bi bi-file-earmark-text me-2"></i>草稿箱
                            </button>
                        </div>
                        <div class="col-md-4 text-end">
                            <i class="bi bi-pen" style="font-size: 4rem; color: #6c757d; opacity: 0.3;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 数据统计 -->
    <div class="row mb-4">
        <div class="col-md-3 mb-3">
            <div class="card stats-card text-center p-3">
                <div class="stats-number" id="totalNovels">0</div>
                <div>总作品数</div>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="card stats-card text-center p-3">
                <div class="stats-number" id="totalChapters">0</div>
                <div>总章节数</div>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="card stats-card text-center p-3">
                <div class="stats-number" id="totalReads">0</div>
                <div>总阅读量</div>
            </div>
        </div>
        <div class="col-md-3 mb-3">
            <div class="card stats-card text-center p-3">
                <div class="stats-number" id="totalFans">0</div>
                <div>粉丝数量</div>
            </div>
        </div>
    </div>

    <!-- 作品管理 -->
    <div class="row">
        <div class="col-12">
            <h3 class="section-title">我的作品</h3>
            <div class="card workspace-card shadow-sm">
                <div class="card-header bg-white border-0 p-3">
                    <div class="row align-items-center">
                        <div class="col-md-6">
                            <div class="input-group">
                                <span class="input-group-text bg-light border-0">
                                    <i class="bi bi-search"></i>
                                </span>
                                <input type="text" class="form-control border-0 bg-light" 
                                       placeholder="搜索作品..." id="searchNovels">
                            </div>
                        </div>
                        <div class="col-md-6 text-end">
                            <select class="form-select d-inline-block w-auto me-2" id="statusFilter">
                                <option value="">全部状态</option>
                                <option value="连载中">连载中</option>
                                <option value="已完结">已完结</option>
                                <option value="暂停">暂停</option>
                            </select>
                            <button class="btn btn-outline-primary action-btn" onclick="refreshNovels()">
                                <i class="bi bi-arrow-clockwise"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="card-body p-0">
                    <div id="novelsContainer">
                        <!-- 作品列表将通过JavaScript动态加载 -->
                    </div>
                    <div id="emptyNovels" class="text-center py-5" style="display: none;">
                        <i class="bi bi-book" style="font-size: 3rem; color: #6c757d;"></i>
                        <h5 class="mt-3 text-muted">还没有作品</h5>
                        <p class="text-muted">开始您的创作之旅吧！</p>
                        <button class="btn quick-action" onclick="showCreateNovelModal()">
                            <i class="bi bi-plus-circle me-2"></i>创建第一部作品
                        </button>
                    </div>
                    <!-- 添加分页容器 -->
                    <div id="pagination-container" class="mt-3 d-flex justify-content-center"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 创建作品模态框 -->
<div class="modal fade" id="createNovelModal" tabindex="-1" aria-labelledby="createNovelModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="createNovelModalLabel">创建新作品</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="createNovelForm">
                    <div class="row">
                        <div class="col-md-8">
                            <div class="mb-3">
                                <label for="novelTitle" class="form-label">作品标题 *</label>
                                <input type="text" class="form-control" id="novelTitle" name="novelTitle" 
                                       required maxlength="50" placeholder="请输入作品标题">
                            </div>
                            <div class="mb-3">
                                <label class="form-label">作品分类 *</label>
                                <div id="novelCategory" class="border rounded p-3" style="max-height: 200px; overflow-y: auto;">
                                    <!-- 分类复选框将通过JavaScript动态加载 -->
                                </div>
                                <div class="form-text">可选择多个分类</div>
                            </div>
                            <div class="mb-3">
                                <label for="novelTags" class="form-label">标签</label>
                                <input type="text" class="form-control" id="novelTags" name="novelTags" 
                                       placeholder="请输入标签，用逗号分隔">
                                <div class="form-text">例如：热血,冒险,成长</div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="mb-3">
                                <label for="novelCover" class="form-label">作品封面</label>
                                <div class="text-center">
                                    <img id="coverPreview" src="https://mysteriouslamb.oss-cn-hangzhou.aliyuncs.com/icon.jpg" 
                                         alt="封面预览" class="img-fluid rounded mb-2" style="max-height: 200px;">
                                    <input type="file" class="form-control" id="novelCover" name="novelCover" 
                                           accept="image/*" onchange="previewCover(this)">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label for="novelIntro" class="form-label">作品简介 *</label>
                        <textarea class="form-control" id="novelIntro" name="novelIntro" rows="4" 
                                  required maxlength="500" placeholder="请输入作品简介，让读者了解您的作品"></textarea>
                        <div class="form-text">简介不超过500字</div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="createNovel()">创建作品</button>
            </div>
        </div>
    </div>
</div>

<!-- 在章节管理模态框之前添加编辑作品模态框 -->
<!-- 编辑作品模态框 -->
<div class="modal fade" id="editNovelModal" tabindex="-1" aria-labelledby="editNovelModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editNovelModalLabel">编辑作品</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="editNovelForm">
                    <input type="hidden" id="editNovelId" name="novelId">
                    <div class="mb-3">
                        <label for="editNovelTitle" class="form-label">作品标题 *</label>
                        <input type="text" class="form-control" id="editNovelTitle" name="novelTitle" 
                               required maxlength="50" placeholder="请输入作品标题">
                    </div>
                    <div class="mb-3">
                        <label for="editNovelTags" class="form-label">作品标签</label>
                        <input type="text" class="form-control" id="editNovelTags" name="novelTags" 
                               placeholder="请输入标签，用逗号分隔">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">作品分类 *</label>
                        <div id="editNovelCategory" class="border rounded p-3" style="max-height: 200px; overflow-y: auto;">
                            <!-- 分类选项将通过JavaScript动态加载 -->
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editNovelStatus" class="form-label">作品状态</label>
                                <select class="form-select" id="editNovelStatus" name="novelStatus">
                                    <option value="连载中">连载中</option>
                                    <option value="已完结">已完结</option>
                                    <option value="暂停">暂停</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="editNovelCover" class="form-label">作品封面</label>
                                <div class="text-center">
                                    <img id="editCoverPreview" src="" 
                                         alt="封面预览" class="img-fluid rounded mb-2" style="max-height: 200px;">
                                    <input type="file" class="form-control" id="editNovelCover" name="novelCover" 
                                           accept="image/*" onchange="previewEditCover(this)">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label for="editNovelIntro" class="form-label">作品简介 *</label>
                        <textarea class="form-control" id="editNovelIntro" name="novelIntro" rows="4" 
                                  required maxlength="500" placeholder="请输入作品简介，让读者了解您的作品"></textarea>
                        <div class="form-text">简介不超过500字</div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="updateNovel()">保存修改</button>
            </div>
        </div>
    </div>
</div>

<!-- 章节管理模态框 -->
<div class="modal fade" id="chapterModal" tabindex="-1" aria-labelledby="chapterModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="chapterModalLabel">章节管理</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h6>章节列表</h6>
                    <button class="btn btn-sm btn-primary" onclick="createNewChapter()">
                        <i class="bi bi-plus"></i> 新增章节
                    </button>
                </div>
                <div id="chapterList" style="max-height: 500px; overflow-y: auto;">
                    <!-- 章节列表将通过JavaScript动态加载 -->
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        crossorigin="anonymous"></script>
<!-- 引入作者工作台JS -->
<script src="${pageContext.request.contextPath}/MainPage/functionPage/authorWorkspace.js"></script>
</body>
</html>